/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unc.pds.model;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import javax.naming.InitialContext;
import unc.pds.data.Consts;
import unc.pds.data.RootDataBeanRemote;
import unc.pds.data.RootDataBeanRemoteHome;
import unc.pds.filemodel.AlbumRemoteHome;
import unc.pds.filemodel.FileRemoteHome;
import unc.pds.filemodel.FolderRemoteHome;
import unc.pds.filemodel.PhotoRemoteHome;
import unc.pds.util.Arch;

/**
 *
 * @author ain
 */
public class ModelFactory {

    private static Model getByBean(RootDataBeanRemote rdbr, Long sessionKey) throws RemoteException {
        try {
            String type = rdbr.getObjectTypeName();
            rdbr.setSessionKey(sessionKey);
            //if (Consts.DEBUG_MODE) System.out.println("ModelFactory.getbykey for " + key + " type is " + type);
            if (type.compareTo(Consts.OBJECT_TYPE__USER) == 0) {
                UserRemoteHome urm = (UserRemoteHome) (new InitialContext()).lookup("ejb/User");
                return urm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__DIARY) == 0) {
                DiaryRemoteHome drm = (DiaryRemoteHome) (new InitialContext()).lookup("ejb/Diary");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__COMMENT) == 0) {
                CommentRemoteHome drm = (CommentRemoteHome) (new InitialContext()).lookup("ejb/Comment");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__FOLDER) == 0) {
                FolderRemoteHome drm = (FolderRemoteHome) (new InitialContext()).lookup("ejb/Folder");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__FILE) == 0) {
                FileRemoteHome drm = (FileRemoteHome) (new InitialContext()).lookup("ejb/File");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__ALBUM) == 0) {
                AlbumRemoteHome drm = (AlbumRemoteHome) (new InitialContext()).lookup("ejb/Album");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__PHOTO) == 0) {
                PhotoRemoteHome drm = (PhotoRemoteHome) (new InitialContext()).lookup("ejb/Photo");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__GROUP) == 0) {
                GroupRemoteHome drm = (GroupRemoteHome) (new InitialContext()).lookup("ejb/Group");
                return drm.create(rdbr, sessionKey);
            }
            if (type.compareTo(Consts.OBJECT_TYPE__MARGROUP) == 0) {
                GroupRemoteHome drm = (GroupRemoteHome) (new InitialContext()).lookup("ejb/Group");
                return drm.create(rdbr, sessionKey);
            }
        } catch (Exception e) {
            throw new RemoteException("ERROR: ModelFactory.getByKey: " + e.getLocalizedMessage());
        }
        return null;
    }

    public static Collection getByParentKey(Long key, Long sessionKey) throws RemoteException {
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            Collection rdbr = rdbrh.findByParentKey(key);
            Collection ans = new ArrayList();
            for (int i = 0; i< rdbr.size(); i++) {
                ans.add(getByBean((RootDataBeanRemote) rdbr.toArray()[i], sessionKey));
            }
            return ans;
        } catch (Exception e) {
            throw new RemoteException("ERROR: ModelFactory.getByKey: " + e.getLocalizedMessage());
        }
    }

    public static Collection getByParentKey(Long key, String type, Long sessionKey) throws RemoteException {
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            Collection rdbr = rdbrh.findByParentKey(key, type);
            Collection ans = new ArrayList();
            for (int i = 0; i< rdbr.size(); i++) {
                ans.add(getByBean((RootDataBeanRemote) rdbr.toArray()[i], sessionKey));
            }
            return ans;
        } catch (Exception e) {
            throw new RemoteException("ERROR: ModelFactory.getByKey: " + e.getLocalizedMessage());
        }
    }

    public static unc.pds.model.Model getByKey(Long key, Long sessionKey) throws RemoteException {
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            RootDataBeanRemote rdbr = rdbrh.findByPrimaryKey(key);
            return getByBean(rdbr, sessionKey);
        } catch (Exception e) {
            throw new RemoteException("ERROR: ModelFactory.getByKey: " + e.getLocalizedMessage());
        }
    }

    public static unc.pds.model.Model getByKey(Long key, String type, Long sessionKey) throws RemoteException {
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            RootDataBeanRemote rdbr = rdbrh.findByPrimaryKey(key, type);
            return getByBean(rdbr, sessionKey);
        } catch (Exception e) {
            throw new RemoteException("ERROR: ModelFactory.getByKey: " + e.getLocalizedMessage());
        }
    }

    public static unc.pds.model.Model getByFieldSet(Arch doc, String type, Long sessionKey) throws RemoteException {
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            RootDataBeanRemote rdbr = rdbrh.findByFieldSet(doc, type);
            return getByBean(rdbr, sessionKey);
        } catch (Exception e) {
            throw new RemoteException("ModelFactory.getByFieldSet: " + e.getLocalizedMessage());
        }
    }
}
